package com.ws.shop.action.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Hashtable;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ws.shop.entity.AdminMenu;
import com.ws.shop.entity.AdminUser;
import com.ws.shop.service.AsynGetTreeMenuService;
import com.ws.shop.util.RequestUtil;

@Controller
@RequestMapping(value="/admin/dhtmltree/*")
public class DHtmlTreeAction extends BaseAdminAction{
	/**
	 * 权限树生成类
	 */
	private static final long serialVersionUID = -8009033248614449239L;
	public final static Hashtable<Integer,Map<Integer,AdminMenu>> ROLE_MENUS_MAP=new Hashtable<Integer,Map<Integer,AdminMenu>>();
	@Resource(name="asynGetTreeMenuService")
	private AsynGetTreeMenuService asynGetTreeMenuService=null;
	
	@RequestMapping(value="gettree.jhtml",method=RequestMethod.GET)
	public String gettree(HttpServletRequest request,HttpServletResponse response) throws IOException, ParserConfigurationException, TransformerException{
		HttpSession session = request.getSession(false);
		if(session == null){
			return null;
		}
		AdminUser admin = getLoginAdmin(session);
		if(admin == null)
			return null;
		response.setContentType("text/xml; charset=utf-8");
		PrintWriter out=response.getWriter();
		Map<Integer,AdminMenu> menumap=ROLE_MENUS_MAP.get(admin.getRoleid());
		if(menumap == null){
			menumap = asynGetTreeMenuService.getTreemenuByRoleid(admin.getRoleid());
			ROLE_MENUS_MAP.put(admin.getRoleid(), menumap);
		}
		asynGetTreeMenuService.createxml(menumap, out,0);
		return null;
	}

	@RequestMapping(value="gettree1.jhtml",method=RequestMethod.GET)
	public String gettree1(HttpServletRequest request,HttpServletResponse response) throws IOException, ParserConfigurationException, TransformerException{
		int id=RequestUtil.getIntParameter(request, "id",0);
		HttpSession session = request.getSession(false);
		if(session == null){
			return null;
		}
		AdminUser admin = getLoginAdmin(session);
		if(admin == null)
			return null;
		response.setContentType("text/xml; charset=utf-8");
		PrintWriter out=response.getWriter();
		Map<Integer,AdminMenu> menumap=ROLE_MENUS_MAP.get(admin.getRoleid());
		if(menumap == null){
			menumap = asynGetTreeMenuService.getTreemenuByRoleid(admin.getRoleid());
			ROLE_MENUS_MAP.put(admin.getRoleid(), menumap);
		}
		asynGetTreeMenuService.createxml(menumap, out,id);
		return null;
	}
	
	@RequestMapping(value="getpermtree.jhtml",method=RequestMethod.GET)
	public String getpermtree(HttpServletRequest request,HttpServletResponse response) throws Exception{
		int roleid=RequestUtil.getIntParameter(request, "roleid", -1);
		response.setContentType("text/xml; charset=utf-8");
		PrintWriter out=response.getWriter();
		Map<Integer,AdminMenu> menumap=ROLE_MENUS_MAP.get(roleid);
		if(menumap == null){
			menumap = asynGetTreeMenuService.getTreemenuByRoleid(roleid);
			ROLE_MENUS_MAP.put(roleid, menumap);
		}
		asynGetTreeMenuService.createxml_qx(menumap, out,0);
		return null;
	}
	
}
